package com.sfac.springboot.modules.common.controller;


import com.sfac.springboot.modules.common.vo.ImageType;
import com.sfac.springboot.modules.common.vo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class EnumDictionaryController {
    //将枚举放到一个静态map对象中，key是枚举名字，value是枚举类模板

    public static Map<String,Class> map = new HashMap() {{
        put("imageType", ImageType.class);
        put("resultStatus", Result.ResultStatus.class);
    }};


    /**
     * 127.0.0.1/api/dictionary/imageType  ----get
     */
    @GetMapping(value = "/dictionary/{enumName}")
    public List<Object> enumDictionary(@PathVariable String enumName) {
        try {


            //
            Class clazz = map.get(enumName);
            if (clazz != null) {
                //
                Method method = clazz.getDeclaredMethod("values");
                Object[] objects =(Object[]) method.invoke(null);

                //
                return Arrays.asList(objects);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
